package com.example.ceshi;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatEditText;

public class EditTextSearch extends AppCompatEditText {

    private Drawable clearDrawable,searchDrawable,voiceDrawable;
    private Context context;

    private boolean isShowClear;

    public EditTextSearch(Context context) {
        super(context);
        this.context = context;
        init();

    }

    public EditTextSearch(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public EditTextSearch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }


    private void init() {
        clearDrawable = getResources().getDrawable(R.drawable.delete_icon);
        searchDrawable = getResources().getDrawable(R.drawable.search_icon);
        voiceDrawable = getResources().getDrawable(R.drawable.voice_icon);
        setCompoundDrawablesWithIntrinsicBounds(searchDrawable, null,
                voiceDrawable, null);

    }




    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        setClearIconVisible(hasFocus() && text.length() > 0);
        // hasFocus()返回是否获得EditTEXT的焦点，即是否选中
        // setClearIconVisible（） = 根据传入的是否选中 & 是否有输入来判断是否显示删除图标->>关注1
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        setClearIconVisible(focused && length() > 0);
        // focused = 是否获得焦点
        // 同样根据setClearIconVisible（）判断是否要显示删除图标
    }


    private void setClearIconVisible(boolean visible) {
        isShowClear = visible;
        setCompoundDrawablesWithIntrinsicBounds(searchDrawable, null,
                visible ? clearDrawable : voiceDrawable, null);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            // 原理：当手指抬起的位置在删除图标的区域，即视为点击了删除图标 = 清空搜索框内容
            // isShowClear：为true,点击删除图标；为false，点击语音图标
            case MotionEvent.ACTION_UP:
                if (isShowClear){
                    Drawable drawable = clearDrawable;
                    if (drawable != null && event.getX() <= (getWidth() - getPaddingRight())
                            && event.getX() >= (getWidth() - getPaddingRight() - drawable.getBounds().width())) {
                        setText("");
//                        Toast.makeText(context,"点击了清除图标",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Drawable drawable = voiceDrawable;
                    if (drawable != null && event.getX() <= (getWidth() - getPaddingRight())
                            && event.getX() >= (getWidth() - getPaddingRight() - drawable.getBounds().width())) {
//                        Toast.makeText(context,"点击了语音图标",Toast.LENGTH_SHORT).show();
                    }
                }


                break;
        }
        return super.onTouchEvent(event);
    }

}

